#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
using namespace std;
int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* run(void *args)
{
    pthread_detach(pthread_self());
    uint64_t number = *(uint64_t*)args;
    cout << "pthread-" << number << "create sucess" << endl;
    while(1)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);//自动释放持有的 mutex 锁 
        cout << "pthread:" << number << ",cnt :" << cnt ++ << endl;
        pthread_mutex_unlock(&mutex);
    }
}
int main()
{
    for(uint64_t i = 0; i < 5; i ++)
    {
        pthread_t tid;
        uint64_t* arg = (uint64_t*)malloc(sizeof(uint64_t));
        *arg = i;  // 将i的值存入动态内存
        pthread_create(&tid,nullptr,run,arg);
        usleep(1000);
    }
    sleep(3);
    while(1)
    {
        sleep(1);
        pthread_cond_signal(&cond);
        cout << "signal one thread..." << endl;
    }
    return 0;
}